package com.example.demo2;

import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author: yhb
 * @Date: Created in 2021/9/28 14:22
 * @Version:1.0
 */
@Service

@Slf4j
public class BankService {

    @Resource
    private BankMapper bankMapper;
    @Resource
    private RestTemplate restTemplate;

    @Transactional
    @LcnTransaction(propagation = DTXPropagation.REQUIRED) //分布式事务注解,发起方使用REQUIRED，事物参与方使用SUPPORTS
    public void add() {
        Bank bank = bankMapper.selectById(1);
        Integer money = bank.getMoney();
        bank.setMoney(money - 100);
        bankMapper.updateById(bank);
        String forObject = restTemplate.getForObject("http://192.1.2.147:9010/add", String.class);
        log.info("{}---{}---{}",forObject);
    }
}
